package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.luyan.LuYanDeatilActivity;
import com.example.administrator.szb.activity.supei.SuPeiDeatilActivity;
import com.example.administrator.szb.activity.teseservice.TeseDeatils;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.MessageBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.fragments.fragment_forTab.home.FragmentHome;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by D N on 2017/11/11.
 */

public class MessageActivity extends BaseActivity {
    @Bind(R.id.message_recyclerview)
    RecyclerView messageRecyclerview;
    @Bind(R.id.message_swiperefreshlayout)
    SwipeRefreshLayout messageSwiperefreshlayout;

    MessageBean messageBean;
    BaseAdapter<MessageBean.DataBean> adapter;

    View footerview;

    ArrayList<MessageBean.DataBean> mdata = new ArrayList<>();
    @Bind(R.id.message_view_show)
    View messageViewShow;

    int wz=0;//用于判断是否加了head,而导致position 的位置不对
    View error_head;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_activity);
        SPUtils.setDatas(SPUtils.SHOW_MSG,"no");
        FragmentHome.isHaveMessage = false;
        ButterKnife.bind(this);
        initToolbar(R.id.message_toolbar_include, "消息管理");
        SampleApplicationLike.getActivitiesInstance().add(this);
        footerview = LayoutInflater.from(this).inflate(R.layout.view_wd_footer, null);
        footerview.setVisibility(View.INVISIBLE);
        initView();
        requestData();
       // FragmentHome.messageCallBack.changeStatu(0);//改变消息
    }

    private void initView() {
        initRefreshLayout();
        //initRecyclerview();
    }

    private void initRefreshLayout() {
        messageSwiperefreshlayout.setColorSchemeResources(R.color.zhutise);
        messageSwiperefreshlayout.post(new Runnable() {
            @Override
            public void run() {
                messageSwiperefreshlayout.setRefreshing(true);
                requestData();
            }
        });

        messageSwiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                messageSwiperefreshlayout.setRefreshing(true);
                requestData();
            }
        });
    }

    private void requestData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        String url = URLAddress.URLBASE + URLAddress.URL_MESSAGE;
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, MessageBean.class, url, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                messageBean = (MessageBean) response;
                findViewById(R.id.message_net_error_include).setVisibility(View.GONE);
                if (messageBean.getResult() == 1) {
                    mdata.clear();

                    mdata.addAll(messageBean.getData());
                    messageSwiperefreshlayout.setRefreshing(false);
                    initRecyclerview();
                    //Toasts.show(MessageActivity.this, "刷新成功", Toast.LENGTH_SHORT);
                }

                adapter.notifyDataSetChanged();


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                messageSwiperefreshlayout.setRefreshing(false);
                findViewById(R.id.message_net_error_include).setVisibility(View.VISIBLE);
                findViewById(R.id.message_net_error_include).findViewById(R.id.home_fragment_button_djcs_error).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        requestData();
                    }
                });
            }
        });
    }

    private void initRecyclerview() {

        messageRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<MessageBean.DataBean>(this, mdata) {
            @Override
            public void convert(int position, BaseViewHolder holder, MessageBean.DataBean dataBean) {
                holder.getView(R.id.textView173).setVisibility(View.GONE);
                if (dataBean.getType()==0){
                    if (dataBean.getIndent_id()!=0) {
                        ((ImageView) holder.getView(R.id.imageView25)).setImageResource(R.drawable.message_xm);
                        ((TextView) holder.getView(R.id.textView170)).setText("项目消息");

                    } else if (dataBean.getRefer_id()!=0) {
                        ((ImageView) holder.getView(R.id.imageView25)).setImageResource(R.drawable.message_wd);
                        //((TextView)holder.getView(R.id.textView170)).setText();
                        ((TextView) holder.getView(R.id.textView170)).setText("问答消息");


                    } else{
                        ((ImageView) holder.getView(R.id.imageView25)).setImageResource(R.drawable.message_rz);
                        ((TextView) holder.getView(R.id.textView170)).setText("认证消息");
                    }
                }else if (dataBean.getType()==1){
                    ((ImageView) holder.getView(R.id.imageView25)).setImageResource(R.drawable.message_xm);
                    ((TextView) holder.getView(R.id.textView170)).setText("速配项目消息");
                }else if (dataBean.getType()==2){
                    ((ImageView) holder.getView(R.id.imageView25)).setImageResource(R.drawable.message_xm);
                    ((TextView) holder.getView(R.id.textView170)).setText("路演消息");
                }else if (dataBean.getType()==3){
                    ((ImageView) holder.getView(R.id.imageView25)).setImageResource(R.drawable.message_xm);
                    ((TextView) holder.getView(R.id.textView170)).setText("特色服务消息");
                }



                ((TextView) holder.getView(R.id.textView171)).setText(dataBean.getMsg());
                ((TextView) holder.getView(R.id.textView172)).setText(dataBean.getCreate_time());

            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.view_message_item;
            }
        };
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MessageBean.DataBean dataBean = messageBean.getData().get(position);
                if (dataBean.getType()==0){
                    if (dataBean.getIndent_id()!=0) {
                        //项目消息
                        if (messageBean.getData().get(position).getGenre()==1){
                            Intent intent = new Intent(MessageActivity.this,ProductDetailActivity.class);
                            intent.putExtra("dd_id",messageBean.getData().get(position).getIndent_id());
                            intent.putExtra("genre",1);
                            startActivity(intent);
                        }else {
                            Intent intent = new Intent(MessageActivity.this,ProductDetail2Activity.class);
                            intent.putExtra("dd_id",messageBean.getData().get(position).getIndent_id());
                            intent.putExtra("genre",2);
                            startActivity(intent);
                        }

                    } else if (dataBean.getRefer_id()!=0) {
                        //问答消息
                        Intent intent = new Intent(MessageActivity.this, WTContentActivity.class);
                        intent.putExtra("refer_id", mdata.get(position).getRefer_id());
                        startActivity(intent);
                    } else{
                        //认证消息
                    }
                }else if (dataBean.getType()==1){
                    Intent intent = new Intent(MessageActivity.this, SuPeiDeatilActivity.class);
                    intent.putExtra("id", dataBean.getOther_id());
                    startActivity(intent);
                }else if (dataBean.getType()==2){
                    Intent intent = new Intent(MessageActivity.this, LuYanDeatilActivity.class);
                    intent.putExtra("id", dataBean.getOther_id());
                    startActivity(intent);
                }else if (dataBean.getType()==3){
                    Intent intent = new Intent(MessageActivity.this, TeseDeatils.class);
                    intent.putExtra("id", dataBean.getOther_id());
                    startActivity(intent);
                }


            }
        });


        if (mdata.size() == 0) {
            error_head = LayoutInflater.from(MessageActivity.this).inflate(R.layout.view_item_error, messageRecyclerview, false);
            ImageView nodata_img = error_head.findViewById(R.id.nodata_img);
            nodata_img.setImageResource(R.mipmap.no_msg_icon);
            adapter.addHeaderView(error_head);
            messageViewShow.setVisibility(View.GONE);
            wz = 1;
        }
        messageRecyclerview.setAdapter(adapter);
    }
    /**
     * 测试异常，重启app
     */
    private void test(){
        final TextView test = null;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                test.setText("sssss");
            }
        }).start();
    }
}
